package com.java.es.cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author jiangli
 * @date 2019/11/21 22:09
 */
public class CglibDemo implements MethodInterceptor {
	@Override
	public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
		System.out.println("代理开始执行~~~");
		Object invokeSuper = methodProxy.invokeSuper(o, objects);
		System.out.println("代理结束执行~~~");
		return invokeSuper;
	}

	public static void main(String[] args) {
		CglibDemo cglibDemo = new CglibDemo();
		//cglib使用asm框架生成代理
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(Buyer.class);
		enhancer.setCallback(cglibDemo);
		Proxy proxy =(Proxy) enhancer.create();
		proxy.sell();
	}
}
